﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Text;

/// <summary>
/// TreeHelper 的摘要说明
/// </summary>
public class TreeHelper
{
    public TreeHelper()
    {

    }

    public static string generateTreeData(int type, string roleid, string agid)
    {
        DBHelper db = new DBHelper();

        string sql = "SELECT ID,typeID,name,parentID,isUsing FROM [Agent] where type=" + type + "  ORDER BY ID ASC";
        if (roleid == "2")
        {
            sql = "SELECT ID,typeID,name,parentID,isUsing FROM [Agent] where id=" + agid + " and type=" + type;
        }

        DataTable dtType = db.getList(sql);

        List<TreeNode> list = new List<TreeNode>();

        foreach (DataRow dr in dtType.Rows)
        {
            TreeNode node = dataRowToTreeNode(dr, dtType);
            list = addNode(node, list);
        }

        string s = Newtonsoft.Json.JsonConvert.SerializeObject(list).Replace(",\"nodes\":[]", "");

        return s;
    }

    private static List<TreeNode> addNode(TreeNode node, List<TreeNode> list)
    {
        if (node.parentID == "0")//顶级节点
        {
            list.Add(node);
        }
        else
        {
            TreeNode tmpNode = list.FirstOrDefault(n => n.tags == node.parentID);
            if (tmpNode != null)
            {
                list[list.IndexOf(tmpNode)].nodes.Add(node);
            }
            else
            {
                for (int i = 0; i < list.Count; i++)
                {
                    if (list[i].nodes.Count > 0)
                    {
                        list[i].nodes = addNode(node, list[i].nodes);
                    }
                }
            }
        }

        return list;
    }

    private static TreeNode dataRowToTreeNode(DataRow dr, DataTable dt)
    {
        TreeNode node = new TreeNode();
        node.text = (dr["isUsing"].ToString() == "True" ? ("" + getTypeNameByID(dr["typeID"].ToString()) + "  " + dr["name"].ToString()) : ("" + getTypeNameByID(dr["typeID"].ToString()) + "  " + dr["name"].ToString() + "(不可用)"));

        if (dr["isUsing"].ToString() == "True")
        {
            node.color = getColor(dr["typeID"].ToString());
        }
        else
        {
            node.color = "red";
        }


        node.tags = dr["ID"].ToString();
        node.parentID = dr["parentID"].ToString();

        return node;
    }

    private static string getTypeNameByID(string typeID)
    {
        string typeName = "";

        switch (typeID)
        {
            case "1": typeName = "<b>①</b>"; break;
            case "2": typeName = "<b>②</b>"; break;
            case "3": typeName = "<b>③</b>"; break;
            case "4": typeName = "<b>④</b>"; break;
        }

        return typeName;
    }

    private static string getColor(string typeID)
    {
        string color = "";

        switch (typeID)
        {
            case "1": color = "blue"; break;
            case "2": color = "green"; break;
            case "3": color = "purple"; break;
            case "4": color = "black"; break;
            default: color = "black"; break;
        }

        return color;
    }
}

public class TreeNode
{
    public string text { get; set; }
    public string color { get; set; }
    public string tags { get; set; }
    public string parentID { get; set; }
    public List<TreeNode> nodes { get; set; }

    public TreeNode()
    {
        nodes = new List<TreeNode>();
    }
}